package IxDLabIdentity;

import processing.core.*;
import processing.opengl.*;

@SuppressWarnings({ "serial", "unused" })
public class GenerativeBackground03 extends PApplet {
	// Get screen resolution
	public final int WIDTH = 800;
	public final int HEIGHT = 600;

	// Set default background
	public final int BACKGROUNDCOLOR = color(232, 230, 225);

	private int[] goldenNumbers = { 3, 7, 10, 17 };
	private int goldenNumber = 0;
	private int lastGoldenNumber = 0;
	private int goldenNumberSum = 0;

	private int minLength = 100;
	private int maxLength = 800;

	private float[] reds = { 17f, 2f, 138f, 0f, 64f };
	private float[] greens = { 132f, 156f, 73f, 164f, 152f };
	private float[] blues = { 196f, 162f, 156f, 197f, 180f };
	private int colorPos = 0;
	private int lastColorPos = 0;

	public void setup() {
		size(WIDTH, HEIGHT, OPENGL);
		background(BACKGROUNDCOLOR);
		
		noStroke();
	}

	public void draw() {
		blendMode(BLEND);
		background(BACKGROUNDCOLOR);

		pushMatrix();
		rotate(-PI / 4);

		while (goldenNumberSum < width) {
			while (goldenNumber == lastGoldenNumber) {
				goldenNumber = goldenNumbers[(int) (random(goldenNumbers.length))];
			}

			translate(0, goldenNumber);

			int rectX = goldenNumberSum;
			int rectY = 0;
			int rectWidth = goldenNumber;
			int rectHeight = floor(random(minLength, maxLength));

			while (colorPos == lastColorPos) {
				colorPos = floor(random(reds.length));
			}
			float rectRed = reds[colorPos];
			float rectGreen = greens[colorPos];
			float rectBlue = blues[colorPos];

			int rectColor = color(rectRed, rectGreen, rectBlue, 230);
			int rectColorAlpha0 = color(rectRed, rectGreen, rectBlue, 0);

			blendMode(BLEND);
			beginShape(QUADS);
			fill(rectColor);
			vertex(rectX, rectY);
			fill(rectColorAlpha0);
			vertex(rectX, rectHeight);
			vertex(rectX + rectWidth, rectHeight);
			fill(rectColor);
			vertex(rectX + rectWidth, rectY);
			endShape(CLOSE);

			goldenNumberSum += goldenNumber;
			lastGoldenNumber = goldenNumber;
			lastColorPos = colorPos;
		}
		goldenNumberSum = 0;

		popMatrix();
		pushMatrix();
		rotate(PI / 4);

		while (goldenNumberSum < width) {
			while (goldenNumber == lastGoldenNumber) {
				goldenNumber = goldenNumbers[(int) (random(goldenNumbers.length))];
			}

			translate(0, -goldenNumber);

			int rectX = goldenNumberSum;
			int rectY = 0;
			int rectWidth = goldenNumber;
			int rectHeight = floor(random(minLength, maxLength));

			while (colorPos == lastColorPos) {
				colorPos = floor(random(reds.length));
			}
			float rectRed = reds[colorPos];
			float rectGreen = greens[colorPos];
			float rectBlue = blues[colorPos];

			int rectColor = color(rectRed, rectGreen, rectBlue, 200);
			int rectColorAlpha0 = color(rectRed, rectGreen, rectBlue, 0);

			blendMode(BLEND);
			beginShape(QUADS);
			fill(rectColor);
			vertex(rectX, rectY);
			fill(rectColorAlpha0);
			vertex(rectX, rectHeight);
			vertex(rectX + rectWidth, rectHeight);
			fill(rectColor);
			vertex(rectX + rectWidth, rectY);
			endShape(CLOSE);

			goldenNumberSum += goldenNumber;
			lastGoldenNumber = goldenNumber;
			lastColorPos = colorPos;
		}
		goldenNumberSum = 0;
		save("background2.png");
		noLoop();
	}

	/*** Event handlers ***/
	public void mousePressed() {
		if (mouseButton == LEFT) {
		} else if (mouseButton == RIGHT) {
		} else {
		}
	}

	public void mouseReleased() {
	}

	public void mouseMoved() {
	}

	public void mouseDragged() {
	}

	public void keyPressed() {
	}
}